package com.xcc.opendd;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bigkoo.pickerview.TimePickerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements TimeAdapter.OnAdapterClickListener {
    private TimePickerView timePickerView1;
    private List<TimeMode> modeList;
    private TimeAdapter adapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        System.out.println("--开启MainActivity--");

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        modeList = new ArrayList<>();
        adapter = new TimeAdapter(getActivity(), modeList).setOnAdapterClickListener(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        findViewById(R.id.imgAdd).setOnClickListener(v -> {
            if (timePickerView1 == null) {
                timePickerView1 = new TimePickerView.Builder(getActivity(), (date, v3) -> {
                    TakeManage.getInstance().addTime(date);
                    showList();
                }).setLayoutRes(R.layout.layout_select_time, v2 ->
                        v2.findViewById(R.id.textConfirm).setOnClickListener(v1 -> {
                            timePickerView1.returnData();
                            timePickerView1.dismiss();
                        })
                ).setType(new boolean[]{false, false, false, true, true, false})
                        .build();
            }
            timePickerView1.show();
        });
    }

    protected boolean isF = true;

    public void onWindowFocusChanged(boolean hasFocus) {
        if (isF & hasFocus) {
            isF = false;
            asynRun();
        }
    }

    private MainActivity getActivity() {
        return this;
    }

    private void asynRun() {
        boolean b = AppUtil.checkApplication(getActivity(), AppLive.DDAppPage);
        if (!b) {
            new AlertDialog.Builder(getActivity())
                    .setMessage("钉钉未安装")
                    .setPositiveButton("确定", null)
                    .show();
            return;
        }
        showList();
        //AppUtil.openAppByPackageName(getActivity(), AppLive.DDAppPage);
    }

    private void showList() {
        List<TimeMode> list = TakeManage.getInstance().getList();
        modeList.clear();
        modeList.addAll(list);
        adapter.notifyDataSetChanged();
    }

    public void onClick(TimeMode mode) {
        new AlertDialog.Builder(getActivity())
                .setMessage("确定删除?")
                .setPositiveButton("确定", (dialog, which) -> {
                    TakeManage.getInstance().delete(mode);
                    showList();
                })
                .setNegativeButton("取消", null)
                .show();
    }

    public void onBackPressed() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }
}
